"""
参数处理函数，发起请求函数
"""
import random
from .partner_model import get_city_name_model, get_area_name_model, add_work_order_model
from .partner_model import get_work_order_model
from copy import deepcopy


def make_work_order_id():
    """
    生成work_order_id
    14位字母和数字组合
    """
    # 造数工具数据前四位XPCS
    work_order_id = "XPCS"
    # 所有大写字母
    letter = [chr(i) for i in range(65, 91)]
    # 数字0-9
    number = list(range(10))
    letter.extend(number)
    work_order_id += ''.join([str(random.choice(letter)) for i in range(10)])
    return work_order_id


def add_work_order_service(json_dict: dict):
    # 复制字典，不对传入字典进行改动
    json_dict = deepcopy(json_dict)

    """接到上游数据后，更新加入 city_name area_name work_order_id"""
    json_dict["city_name"] = get_city_name_model(json_dict.get("city_id"))
    json_dict["area_name"] = get_area_name_model(json_dict.get("area_id"))
    work_order_id = make_work_order_id()
    json_dict["work_order_id"] = work_order_id
    add_work_order_model(json_dict)
    return {"work_order_id": work_order_id}


def get_work_order_service(json_dict: dict):
    # 复制字典，不对传入字典进行改动
    json_dict = deepcopy(json_dict)
    # 分页信息提取
    limit_dict = {"page_size": json_dict.pop("page_size"), "page_num": json_dict.pop("page_num")}
    count, result = get_work_order_model(json_dict, limit_dict)
    return count, result
